import time
import re
import scrapy
from spidertools.utils.time_utils import get_current_date
from commonresources.inner_utils.selenium_utils import SeleniumUtils
from commonresources.spider_items.base_item import convert_dict
from commonresources.spider_items.chongqing.items import ChongQingGongGongZiYuanJiaoYiJianDuWangItem
from commonresources.spiders.basespider import BaseSpider
import scrapy
from spidertools.utils.time_utils import get_current_date
import re
import requests
from commonresources.spider_items.beijing.items import BeiJingShiGongGongZiYuanJiaoYiFuWuPingTaiItem
from commonresources.spiders.basespider import BaseSpider
from scrapy.selector import Selector
import requests.packages.urllib3
import json


class ChongQingGongGongZiYuanJiaoYiJianDuWang(BaseSpider):

    name = "ChongQingGongGongZiYuanJiaoYiJianDuWang"
    name_zh = "重庆公共资源交易监督网"
    province = "重庆"
    start_urls = ["https://www.baidu.com/"]

    def __init__(self, full_dose=False):
        super(ChongQingGongGongZiYuanJiaoYiJianDuWang, self).__init__(full_dose)
        self.convert_dict = convert_dict

    def parse(self, response):
        type_dict={ '招标公告':'002001001',
                    '中标候选人公示':'002001005',
                    '中标公告': '002001003',
                    }
        for key,value in type_dict.items():
            start_url = 'http://ggzyjyjgj.cq.gov.cn/inteligentsearch/rest/inteligentSearch/getFullTextData'
            object_type =key
            content_0 = json.dumps([
                    {"fieldName": "categorynum", "equal": value, "notEqual": 'null', "equalList": 'null',
                     "notEqualList": 'null', "isLike": 'true', "likeType": '2'}])
            UserAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'

            headers = {'User-Agent': UserAgent}
            formdata={
                "token": "",
                 "pn": '20',
                 "rn": '20',
                 "sdt": "",
                 "edt": "", "wd": " ", "inc_wd": "", "exc_wd": "",
                 "fields": "title", "cnum": "002",
                 "sort": "{'istop':0,'ordernum':0,'webdate':0,'publishdate':0,'id':0}", "ssort": "title",
                 "cl": '200', "terminal": "",
                 "condition":content_0,
                 "time": 'null', "highlights": "title",
                 "statistics": 'null', "unionCondition": 'null', "accuracy": "", "noParticiple": "0", "searchRange": 'null',
                 "isBusiness": "1"
            }
            yield scrapy.FormRequest(
                url=start_url,
                headers=headers,
                callback=self.first_parse,
                dont_filter=True,
                formdata=formdata,
                meta={
                    "type": object_type,
                    "need_break": False,

                },
            )


    def first_parse(self,response):
        j=1
        pass



